<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .judgeWrapper {
            width: 600px;
            /* border: 1px solid #333; */
            margin: 100px auto;
        }

        .judgeBox {
            position: relative;
        }

        .judgeBox .boxA,
        .judgeBox .boxB {
            width: 50px;
            height: 50px;
            background-color: #fff;
            position: absolute;
            top: 100px;
        }

        .judgeBox .boxA {
            left: 0;
            background: url(../images/bg.jpg) no-repeat -300px -100px / 600px auto;
            z-index: 1;
        }

        .judgeBox .boxB {
            left: 300px;
        }

        .judgeBox img {
            width: 100%;
            height: 264px;
        }

        .slideBar {
            width: 100%;
            height: 50px;
            position: relative;
            margin-top: 10px;
            background-color: darkgrey;
            border-radius: 25px;
        }

        .ball {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: pink;
            position: absolute;
            left: 0px;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="judgeWrapper">
        <div class="judgeBox">
            <img src="../images/bg.jpg" alt="">
            <div class="boxA"></div>
            <div class="boxB"></div>
        </div>
        <div class="slideBar">
            <div class="ball"></div>
        </div>
    </div>
</body>
<script>
    // 滑动解锁:
    // 1. 拖拽小球 ball 移动
    // 2. 鼠标松开时进行验证 => boxA boxB 重叠 => 验证成功  否则=> 失败 回到原位置
    // 3. 随机位置

    // 问题:
    // judgeBox 依靠图片撑开,但是图片加载时需要时间的, 如果文档(html结构)加载完成,但是图片还没有加载过来 => 没有办法撑开judgeBox

    // 解决方法:
    // 1. 给图片设置定高  =>  无论图片是否加载成功 都是这么高
    // 2. 把代码放到window.onload 中  => 等页面所有内容加载完毕(文档,img,script)在执行




    window.onload = function () {
        var judgeWrapper = document.getElementsByClassName("judgeWrapper")[0];
        var judgeBox = document.getElementsByClassName("judgeBox")[0];
        var slideBar = document.getElementsByClassName("slideBar")[0];
        var ball = document.getElementsByClassName("ball")[0];
        var boxA = document.getElementsByClassName("boxA")[0];
        var boxB = document.getElementsByClassName("boxB")[0];


        var maxLeft = slideBar.clientWidth - ball.clientWidth;
        var maxTop = judgeBox.clientHeight - boxB.clientHeight;
        console.log(judgeBox.clientHeight, boxB.clientHeight);

        randPos()


        ball.onmousedown = function (e) {
            var e = e || window.event;
            var posX = e.offsetX;
            console.log(posX);

            document.onmousemove = function (e) {
                var e = e || window.event;
                var x = e.pageX - judgeWrapper.offsetLeft - posX;

                x = Math.min(Math.max(0, x), maxLeft);

                ball.style.left = x + "px";
                boxA.style.left = x + "px";


            }
            document.onmouseup = function () {
                document.onmousemove = null;
                document.onmouseup = null;

                // 2. 鼠标松开时进行验证 => boxA boxB 重叠 => 验证成功  否则=> 失败 回到原位置
                // 解析: boxA boxB 重叠  => left值相同

                // 理想情况下: boxA.offsetLeft == boxB.offsetLeft  => 对手残党不友好?
                // 设置误差允许范围:  [boxB.offsetLeft-2,boxB.offsetLeft+2]
                // a. boxA.offsetLeft>= boxB.offsetLeft - 2 && boxA.offsetLeft <= boxB.offsetLeft + 2
                // b. Math.abs(boxA.offsetLeft - boxB.offsetLeft) <= 2;

                // 如果boxA移动了再验证
                if (boxA.offsetLeft) {
                    if (Math.abs(boxA.offsetLeft - boxB.offsetLeft) <= 2) {
                        // console.log("解锁成功");
                        location.href = "https://www.baidu.com"
                    } else {
                        ball.style.left = 0 + "px";
                        boxA.style.left = 0 + "px";

                        randPos();


                    }
                }


            }

        }

        function randPos() {

            // x => 200 - maxLeft
            // y => 0 - maxTop
            var randX = 200 + Math.round(Math.random() * (maxLeft - 200));
            var randY = Math.round(Math.random() * maxTop);

            boxB.style.left = randX + "px";
            boxB.style.top = randY + "px";

            boxA.style.top = randY + "px";
            // boxA.style.backgroundPosition = -randX + "px " + (-randY) + "px";
            // boxA.style.backgroundPositionX = -randX + "px ";
            // boxA.style.backgroundPositionY = -randY + "px ";

            boxA.style.backgroundPosition = `-${randX}px -${randY}px`;

        }

    }
</script>

</html>